﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using CID.Tests.Support;
using Rhino.Mocks;

namespace CID.Execution.Controller.Tests
{
    [TestFixture]
    class DeploymentStateRepositoryTests : BaseFunctionalTests
    {
        private DeploymentStateRepository repository;

        private static IDeploymentState CreateDeploymentStateStub()
        {
            var deploymentUniqueIdentifier = Guid.NewGuid();
            var result = MockRepository.GenerateStub<IDeploymentState>();
            result.Stub(x => x.DeploymentUniqueIdentifier).Return(deploymentUniqueIdentifier);
            return result;
        }

        public override void SetUp()
        {
            base.SetUp();
            repository = typeof(DeploymentStateRepository).GetConstructor(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance,
                null, new Type[0], null).Invoke(null) as DeploymentStateRepository;
        }

        #region Add
        [Test]
        public void TestAddOnce_ShouldSuccceed()
        {
            var deploymentState = CreateDeploymentStateStub();

            repository.Add(deploymentState);

            Assert.AreEqual(1, repository.Count);
        }

        [Test]
        public void TestAddTwice_ShouldSuccceed()
        {
            var deploymentState = CreateDeploymentStateStub();

            repository.Add(deploymentState);
            repository.Add(deploymentState);

            Assert.AreEqual(1, repository.Count);
        }

        [Test]
        public void TestAddTwoDifferent_ShouldSuccceed()
        {
            var deploymentState1 = CreateDeploymentStateStub();
            var deploymentState2 = CreateDeploymentStateStub();

            repository.Add(deploymentState1);
            repository.Add(deploymentState2);

            Assert.AreEqual(2, repository.Count);
        }
        #endregion

        #region Remove
        [Test]
        public void TestRemove_DeploymentStatePreviouslyAdded_ShouldBeRemoved()
        {
            var deploymentState1 = CreateDeploymentStateStub();
            var deploymentState2 = CreateDeploymentStateStub();
            repository.Add(deploymentState1);
            repository.Add(deploymentState2);

            repository.Remove(deploymentState1);

            Assert.AreEqual(1, repository.Count);
        }

        [Test]
        public void TestGet_DeploymentStateNonPreviouslyAdded_NothingShouldHappen()
        {
            var deploymentState1 = CreateDeploymentStateStub();
            var deploymentState2 = CreateDeploymentStateStub();
            repository.Add(deploymentState1);

            repository.Remove(deploymentState2);

            Assert.AreEqual(1, repository.Count);
        }
        #endregion

        #region Get
        [Test]
        public void TestGet_DeploymentStatePreviouslyAdded_ShouldSuccceed()
        {
            var deploymentState = CreateDeploymentStateStub();
            repository.Add(deploymentState);

            var result = repository.Get(deploymentState.DeploymentUniqueIdentifier);

            Assert.AreEqual(deploymentState, result);
        }

        [Test]
        public void TestGet_DeploymentStateNonPreviouslyAdded_ShouldReturnNull()
        {
            var deploymentState1 = CreateDeploymentStateStub();
            var deploymentState2 = CreateDeploymentStateStub();
            repository.Add(deploymentState1);

            var result = repository.Get(deploymentState2.DeploymentUniqueIdentifier);

            Assert.IsNull(result);
        }
        #endregion
    }
}
